//    Copyright (C) Kherty.  All rights reserved.
using System;

namespace OpenLS.Spreadsheet.AnalysisToolPak
{
    internal sealed class Nasd30360Basis : ConstantDateBasis
    {
        protected override int DaysInYearImp
        {
            get { return 360; }
        }

        protected override DateBasisType Type
        {
            get { return DateBasisType.Nasd30360; }
        }

        internal override int DaysBetweenImp(DateTime start, DateTime end)
        {
            int startDay = start.Day;
            int endDay = end.Day;
            if (start.Month == 2 && start.Day == DateTime.DaysInMonth(start.Year, start.Month))
                startDay = 30;
            if (endDay == 31 && startDay >= 30)
                endDay = 30;
            if (startDay == 31)
                startDay = 30;
            return (end.Year - start.Year) * 360 + (end.Month- start.Month) * 30 + (endDay - startDay);
        }


        protected override int DayDifferenceImp(DateTime d1, DateTime d2)
        {
            if (d1 < d2)
                return -DayDifferenceImp(d2, d1);
            int months = (d1.Year - d2.Year)*12 + (d1.Month - d2.Month);
            int result = months*30 + (d1.Day - d2.Day);

            if (d2.Month == 2 && d1.Month != 2 && d2.Year == d1.Year)
            {
                if (DateTime.IsLeapYear(d1.Year))
                    return result - 1;
                return result - 2;
            }
            return result;
        }
    }
}